// 发布订阅： 发布与订阅之间没有耦合关系，何时发布取决于自己

const fs = require('fs')
const path = require('path')


let events = {
    _events: [],
    on(cb) {
        this._events.push(cb)
    },
    emit(...args) {
        this._events.forEach(cb => cb(...args))
    }
}

events.on(function (data) {
    console.log(data);
})

let school = {}
events.on(function (key, data) {
    school[key] = data
    if (Object.keys(school).length === 2) {
        console.log(school);
    }
})


fs.readFile(path.resolve(__dirname, 'name.txt'), 'utf-8', function (err, data) {
    events.emit('name', data)
})

fs.readFile(path.resolve(__dirname, 'age.txt'), 'utf-8', function (err, data) {
    events.emit('age', data)
})